package o2o.web.actions.shop;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYMathDoubleUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.shop.ShopWalletMemory;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.shop.ShopWallet;

import java.sql.SQLException;

import static o2o.dao.PO.PojoTables.ShopWallet;

//工人钱包
public class ShopWalletMan {
    public final static int ADD = 1;//增加余额
    public final static int POP = 2;//提现申请
    public final static int POP_SUCCESS = 3;//提现成功
    public final static int POP_REFUSE = 12;// 拒绝提现


    //更新余额
    synchronized public static ResultInfo saveOrUpdateShopWallet(String username, Long userId, int type, Double money, String updateUser) {
        if (ValidateUtil.isNull(username)) {
            return new ResultInfo("账号不能为空");
        }

        if (ValidateUtil.isNotAbsDouble(money)) {
            return new ResultInfo("金额错误");
        }


        ShopWallet shopWallet = ShopWalletMemory.getInstance().getShopWallet(username);
        Long time = DateUtils.getCurrTime();
        switch (type) {
            case ADD: {//增加
                if (null == shopWallet) {//账户不存在
                    //新建
                    shopWallet = new ShopWallet();
                    shopWallet.setShopUsername(username);
                    shopWallet.setShopId(userId);
                    shopWallet.setMoney(money);//余额
                    shopWallet.setTotalMoney(money);//总金额
                    shopWallet.setPopMoney(0d);//提现金额
                    shopWallet.setPopTotalMoney(0d);//提现总金额
                    shopWallet.setDelFlag(0);
                    shopWallet.setCreateUser(updateUser);
                    shopWallet.setCreateTime(time);
                    try {
                        UserDaoSql.getInstance().insertObj(ShopWallet, shopWallet);
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("处理失败，请稍后再试");
                    }
                } else {
                    shopWallet.setMoney(JYMathDoubleUtils.add(shopWallet.getMoney(), money));
                    shopWallet.setTotalMoney(JYMathDoubleUtils.add(shopWallet.getTotalMoney(), money));

                    StringBuilder sb = new StringBuilder();
                    sb.append("update ").append(ShopWallet).append(" set money = ").append(shopWallet.getMoney())
                            .append(",totalMoney=").append(shopWallet.getTotalMoney()).append(",updateUser = '").append(updateUser)
                            .append("' where shopUsername = '").append(username).append("' and delFlag = 0");
                    try {
                        UserDaoSql.getInstance().executeBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("处理失败，请稍后再试");
                    }
                }

                //保存到缓存
                ShopWalletMemory.getInstance().updateShopWallet(username);
            }
            break;
            case POP: {//提现申请
                if (null == shopWallet) {//账户不存在
                    return new ResultInfo("账户不存在");
                }
                //减少的金额是否不大于余额
                if (JYMathDoubleUtils.sub(shopWallet.getMoney(), money) < 0) {
                    return new ResultInfo("提现金额超出余额");
                }

                //减少
                shopWallet.setMoney(JYMathDoubleUtils.sub(shopWallet.getMoney(), money));//余额减少
                shopWallet.setPopMoney(JYMathDoubleUtils.add(shopWallet.getPopMoney(), money));//提现金额增加

                //保存到数据库
                StringBuilder sb = new StringBuilder();
                sb.append("update ").append(ShopWallet).append(" set money = ").append(shopWallet.getMoney())
                        .append(",popMoney = ").append(shopWallet.getPopMoney()).append(",updateUser = '").append(updateUser)
                        .append("' where shopUsername = '").append(username).append("' and delFlag = 0");
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("申请失败，请稍后再试");
                }

                //更新缓存
                ShopWalletMemory.getInstance().updateShopWallet(username);
            }
            break;
            case POP_SUCCESS: {//提现成功
                if (null == shopWallet) {//账户不存在
                    return new ResultInfo("账户不存在");
                }
                //减少的金额是否不大于余额
                if (JYMathDoubleUtils.sub(shopWallet.getPopMoney(), money) < 0) {
                    return new ResultInfo("提现金额超出申请金额");
                }


                shopWallet.setPopMoney(JYMathDoubleUtils.sub(shopWallet.getPopMoney(), money));//减少提现申请金额
                shopWallet.setPopTotalMoney(JYMathDoubleUtils.add(shopWallet.getPopTotalMoney(), money));//增加提现总金额

                //保存到数据库
                StringBuilder sb = new StringBuilder();
                sb.append("update ").append(ShopWallet).append(" set popMoney = ").append(shopWallet.getPopMoney())
                        .append(",popTotalMoney = ").append(shopWallet.getPopTotalMoney()).append(",updateUser = '").append(updateUser)
                        .append("' where shopUsername = '").append(username).append("' and delFlag = 0");
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("申请失败，请稍后再试");
                }

                //更新缓存
                ShopWalletMemory.getInstance().updateShopWallet(username);
            }
            break;
        }


        return new ResultInfo();
    }

}
